/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2023-12-07 09:26:03
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2023-12-19 19:14:32
 * @FilePath: /yt-devops/lib/pages/tasks/install_task_detail/install_task_detail_controller.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/configs.dart';
import 'package:yt_devops/model/task_detail_model.dart';
import 'package:yt_devops/pages/tasks/task_list/task_list_controller.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/routers/router_path.dart';
import 'package:yt_devops/services/task/task_services.dart';
import 'package:yt_devops/utils/enums.dart';

import 'install_task_detail_model.dart';
import 'models/task_install_detail_model.dart';

class InstallTaskDetailController extends GetxController {
  InstallTaskDetailModel model = InstallTaskDetailModel();
  TaskInstallDetailModel detailModel = TaskInstallDetailModel();
  TaskListController taskListController = Get.find();

  final TaskServices services = Get.find();

  late int taskID;

  // 是否可以编辑表单
  bool enableEdit = false;

  @override
  void onInit() {
    super.onInit();
    taskID = Get.arguments['id'];
    loadDetail(taskID);
  }

  @override
  void onReady() {}

  @override
  void onClose() {}

  loadDetail(int id) => Configs.httpClient.requestWithLoading(() async {
        var params = {'id': id};
        var data = await services.getInstallTaskDetail(params);
        model = InstallTaskDetailModel.fromJson(data);
        detailModel = TaskInstallDetailModel.fromJson(data);
        enableEdit =
            (model.status == InstallTaskStatusType.waitingForProcessing)
                ? true
                : false;
        update();
      });

  accept(id, TaskType type) => Configs.httpClient.requestWithLoading(() async {
        await services.acceptInstallTask({'id': id});
        BrnToast.show(
          ResKey.taskProcessSuccessTip.tr,
          Get.context!,
          preIcon: Image.asset(
            ImageAssets.iconSuccess,
            width: 24,
            height: 24,
          ),
          duration: BrnDuration.short,
          onDismiss: () {
            loadDetail(id);
            taskListController.refreshInstall();
          },
        );
      });

  toCreateSite() {
    var params = {'detail': detailModel};
    Log.d('detailModel==' + detailModel.siteName.toString());
    Get.toNamed(RouterPath.taskCreateSite, arguments: params)?.then((value) {
      if (value == true) {
        loadDetail(taskID);
      }
    });
  }

  toFinish(id) => Configs.httpClient.requestWithLoading(() async {
        await services.completeInstall({'id': id});
        BrnToast.show(
          ResKey.workOrderComplete.tr,
          Get.context!,
          preIcon: Image.asset(
            ImageAssets.iconSuccess,
            width: 24,
            height: 24,
          ),
          duration: BrnDuration.short,
          onDismiss: () {
            loadDetail(id);
            taskListController.refreshInstall();
          },
        );
      });
}
